package com.example.food_contract_system.service.impl;

import org.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.food_contract_system.domain.Agency;
import com.example.food_contract_system.mapper.AgencyMapper;
import com.example.food_contract_system.mapper.FreezeMapper;
import com.example.food_contract_system.vo.GetAppealVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.food_contract_system.domain.Appeal;
import com.example.food_contract_system.service.AppealService;
import com.example.food_contract_system.mapper.AppealMapper;
import com.example.food_contract_system.util.R;
import com.example.food_contract_system.vo.InsertAppealVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author Lhy
* @description 针对表【appeal】的数据库操作Service实现
* @createDate 2023-07-14 08:33:44
*/
@Service
public class AppealServiceImpl extends ServiceImpl<AppealMapper, Appeal> implements AppealService{

    @Autowired
    private AppealMapper appealMapper;
    @Autowired
    private AgencyMapper agencyMapper;
    @Autowired
    private FreezeMapper freezeMapper;

    @Transactional
    @Override
    public R insertAppeal(InsertAppealVo insertAppealVo) {

        List<String> hrefList = new ArrayList<>();

        String[] split = insertAppealVo.getAppealHref().replace("[","").replace("\"","").replace("]","").split(",");
        for (String s : split) {
            hrefList.add("http://localhost:8081/index/"+s);
        }

        Appeal appeal = new Appeal();

        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();

        appeal.setAppId(s.substring(0,4));
        appeal.setAppealMsg(insertAppealVo.getAppealMsg());
        appeal.setAId(insertAppealVo.getAId());
        appeal.setAParentId(insertAppealVo.getAParentId());

        StringJoiner joiner = new StringJoiner(",");
        hrefList.forEach(joiner::add);
        String mergedString = joiner.toString();

        appeal.setAppealHref(mergedString);
        appeal.setOId(insertAppealVo.getOId());

        int insert = appealMapper.insertMyself(appeal);
        if (insert != 0){
            return R.ok().put("msg","申诉成功!");
        }
        return R.error().put("msg","请联系管理员");
    }

    @Override
    public R getAppeal(String aId) {
        List<Appeal> appealLists = appealMapper.selectList(new QueryWrapper<Appeal>()
                .eq("a_parent_id", aId));
        if (appealLists.size() > 0){
            List<GetAppealVo> getAppealVos = appealLists.stream().map(appeal -> {
                GetAppealVo getAppealVo = new GetAppealVo();

                getAppealVo.setAppStatus(appeal.getAppStatus());
                getAppealVo.setAppMsg(appeal.getAppealMsg());
                getAppealVo.setAppId(appeal.getAppId());
                getAppealVo.setAId(appeal.getAId());
                getAppealVo.setAppHref(Arrays.asList(appeal.getAppealHref().split(",")));
                getAppealVo.setOId(appeal.getOId());

                Agency agency = agencyMapper.selectOne(new QueryWrapper<Agency>()
                        .eq("a_id", appeal.getAId()));

                if (agency != null) {
                    getAppealVo.setAName(agency.getAName());
                    getAppealVo.setAAccount(agency.getAAccount());
                    getAppealVo.setAAccountStatus(agency.getAAccountStatus());
                }
                return getAppealVo;
            }).collect(Collectors.toList());
            return R.ok().put("data",getAppealVos);
        }
        return R.error();
    }

    @Transactional
    @Override
    public R sendAnswer(String aId, String msg, String appId) {

        JSONObject jsonObj = new JSONObject(msg);
        String msgValue = jsonObj.getString("msg");

        freezeMapper.updateMyself(aId,msgValue);

        appealMapper.updateMyself(appId);
        return R.ok();
    }

    @Transactional
    @Override
    public R deleteAppeal(String appId) {
        appealMapper.deleteMyself(appId);
        return R.ok();
    }
}




